﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Reflection;
using System.IO;



namespace Docentric.Examples.Browser
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
			InitializeComponent();

            ExampleItemsControl.ItemsSource = Examples;
        }


        private List<ExampleBase> _examples = null;
        public IEnumerable<ExampleBase> Examples
        {
            get
            {
                if (_examples == null)
                {
                    _examples = new List<ExampleBase>()
								{
									new SimpleValuesExample(),
									new SimpleValuesXmlExample(),
									new SimpleValuesDtsExample(),
									new FormattingValuesExample(),
									new RenderingCollectionExample(),
									new RenderingCollectionXmlExample(),
									new RenderingCollectionDtsExample(),
									new RenderingCollectionArbitraryContentExample(),
									new RenderingCollectionNestedTableExample(),
									new RenderingCollectionHorizontallyExample(),
									new GroupingExample(),
									new ImagesExample(),
									new ConditionalContentExample(),
									new ChartsExample(),
									new TypedDataSetExample(),
									new PlainDataSetExample(),
									new MergingSubDocumentsExample()
								};
                }

                return _examples;
            }
        }


        private void RadioButton_Checked(object sender, RoutedEventArgs e)
        {
			DemoContentGroupBox.Visibility = Visibility.Visible;
			cc_Welcome.Visibility = Visibility.Collapsed;

			ExampleBase example = (ExampleBase)(((RadioButton)sender).DataContext);
			DemoContentGroupBox.DataContext = example;

			// C# code.
			TextRange textRange = new TextRange(rtb_Code.Document.ContentStart, rtb_Code.Document.ContentEnd);
			Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Docentric.Examples.Browser.ExampleResources." + example.Name + ".CSharpCode.rtf");
			textRange.Load(stream, DataFormats.Rtf);

			// Help.
			cmd_ExampleInHelp.Visibility = string.IsNullOrEmpty(example.HelpTopicPage) ? Visibility.Collapsed : Visibility.Visible;
        }


        private void ButtonReportTemplate_Click(object sender, RoutedEventArgs e)
        {
            ExampleBase example = (ExampleBase)(((Button)sender).DataContext);
			System.Diagnostics.Process.Start(example.TemplateFileName);
        }


        private void ButtonGenerateReport_Click(object sender, RoutedEventArgs e)
        {
            ExampleBase example = (ExampleBase)(((Button)sender).DataContext);

			// Generate the report for the currently selected example.
			try
			{
				string reportDocumentFileName = example.GenerateReport();

				// Show it.
				System.Diagnostics.Process.Start(reportDocumentFileName);
			}
			catch (Exception exc)
			{
				MessageBox.Show(exc.ToString());
			}
        }


		private void cmd_ExampleInHelp_Click(object sender, RoutedEventArgs e)
		{
			ExampleBase example = DemoContentGroupBox.DataContext as ExampleBase;
			if (example == null) return;
			System.Diagnostics.Process.Start("http://www.docentric.com/documentation/page/" + example.HelpTopicPage);
		}


		private void cmd_OpenSampleProjects_Click(object sender, RoutedEventArgs e)
		{
			const string REGISTRY_KEY_PATH = "SOFTWARE\\Docentric\\Toolkit";

			// Get the location of the example browser and launch the application.
			Microsoft.Win32.RegistryKey exampleBrowserBinFolderKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(REGISTRY_KEY_PATH, false);
			string exampleBrowserSrcFolder = (string)exampleBrowserBinFolderKey.GetValue("ExampleBrowserSrcFolder");

			// Open windows explorer.
			System.Diagnostics.Process.Start("explorer.exe", "\"" + exampleBrowserSrcFolder + "\"");
		}
    }
}
